var express = require('express');
var axios = require('axios')
var router = express.Router();
let request = require('request')
const sha1 = require('sha1')
const tmsg = require('tmsg');
const sendTMsg = tmsg.sendTMsg;
let send_wx_msg = require('../utils/wx_msg_tpl')
router.all('/api/wx/share1',async (req,res) =>{
    let url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx838cf54dd030d93a&secret=b7f8aca56652260930307a1597e349ab'


    request(url,function (error, response, data) {

        res.send(data)
    });

});

router.all('/api/wx/share2',function (req,res) {
    let token = req.query.token;
    let url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='+req.query.token+'&type=jsapi'
    request(url,function (error, response, data) {

        res.send(data)
    });

})


router.all('/api/wx/share3',function (req,res) {
    let noncestr='azxrqwrdaf'+new Date().now;
    let jsapi_ticket=req.query.jsapi_ticket;
    let timestamp=new Date().getTime();
    let url = req.query.url;

    let a = "jsapi_ticket="+jsapi_ticket+"&noncestr="+noncestr+"&timestamp="+timestamp+"&url="+url;

    let oppId = 'wx838cf54dd030d93a';
    let signature = sha1(a)

    let result = {
        noncestr,jsapi_ticket,timestamp,signature,oppId,a,url
    }
    res.send(result)


})

router.all('/wx/message',function (req,res) {
    /**
     *
     * @type {User.openid|{type, comment}}
     */
    let select = req.query.select;
    let touser = req.query.openid;
    let Turl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wx838cf54dd030d93a&secret=b7f8aca56652260930307a1597e349ab'
    request(Turl,function (error,response,data) {

        let url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='+(JSON.parse(data)).access_token
        let temList ='https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token='+(JSON.parse(data)).access_token
        let access_token = (JSON.parse(data)).access_token

        request(temList,function (error,response,body) {

            res.json(JSON.parse(body))
            // var d = send_wx_msg.task_create_success("obV1T531QSVBseNf6nXXuq1sr0AU","上海某广告位二维码扫码应用开发需求","需求状态：需求已发布，等待服务商响应")
            let d = {};
            if(select=='task_create_success'){//创建任务提醒
                let first =req.query.first
                let name = req.query.name
                let status = req.query.status
                 d = send_wx_msg.task_create_success(touser,first,name,status)
            }else if(select == 'task_sigin'){//任务授权提醒
                let first = req.query.first;
                let name = req.query.name;
                let status = req.query.status;
                d = send_wx_msg.task_sigin(touser,first,name,status)
            }else if(select == 'task_finish'){//目标完成待确认提醒
                let first = req.query.first;
                let content = req.query.content;
                let type = req.query.type;
                let user = req.query.user;
                let remark = req.query.remark;
                d = send_wx_msg.task_finish(touser,first,content,type,user,remark)
            }else if(select == 'task_finish2'){//验收结果通知
                let first = req.query.first;
                let content = req.query.content;
                let status = req.query.status;
                let user = req.query.user;
                let remark = req.query.remark;
                d = send_wx_msg.task_finish2(touser,first,content,status,user,remark)
            }
            request({
                url: url,
                method: "POST",
                json: true,
                body: d
            },function (error,response,body) {

                // res.send(body)
            })
        })


    })
})
module.exports = router;

